package com.goldcn.classify.controller;

import com.goldcn.classify.model.Classify;
import com.goldcn.classify.service.ClassifyService;
import com.goldcn.classify.service.ClassifyStyleService;
import com.goldcn.classify.service.ClassifyToAttrDefService;
import com.goldcn.common.consts.RestConst;
import com.goldcn.common.exception.ServiceException;
import com.goldcn.common.util.UuidUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2016/5/9.
 */
@RestController
@RequestMapping(value = "/classify")
@Api(value = "商品分类&类目&品类接口")
public class ClassifyController {
    @Autowired
    private ClassifyService classifyService;

    @Autowired
    private ClassifyStyleService classifyStyleService;

    @Autowired
    private ClassifyToAttrDefService classifyToAttrDefService;


    /**
     * 查询对应平台下所有的末端品类（实现code=4.所有）
     *
     * @param code
     * @return
     */
    @RequestMapping(value = "/tipclassify/{code}", method = RequestMethod.GET)
    @ApiOperation(value = "查询对应平台下所有的末端品类")
    public Object loadAllTipClassify(@PathVariable String code) {
        return classifyService.selectTipClassify(code);
    }

    /**
     * 查询对应平台下所有的一级品类（实现code=4.所有; 7：品类关联成色的所有平台 ）
     *
     * @param code
     * @return
     */
    @RequestMapping(value = "/list/{code}", method = RequestMethod.GET)
    @ApiOperation(value = "查询对应平台的一级品类")
    public Object loadClassifyList(@PathVariable String code) {
        return classifyService.selectClassifyList(code);
    }


    /**
     * 根据平台类型获取树结构的所有品类-成色-款式
     *
     * @param code
     * @return
     */
    @RequestMapping(value = "/csclistT/{code}", method = RequestMethod.GET)
    @ApiOperation(value = "根据平台类型获取树结构的所有品类-成色-款式")
    public Object selectColorStyleByClassify(@PathVariable String code,
                                             @RequestParam(value = "clazzID", required = false) String clazzID) {
        return classifyService.selectColorStyleByClassify(code, 1, clazzID);
    }

    /**
     * 根据平台类型获取所有品类-成色-款式
     *
     * @param code
     * @return
     */
    @RequestMapping(value = "/csclist/{code}", method = RequestMethod.GET)
    @ApiOperation(value = "根据平台类型获取所有品类-成色-款式")
    public Object selectColorStyleByClassifyNoTree(@PathVariable String code,
                                                   @RequestParam(value = "clazzID", required = false) String clazzID) {
        return classifyService.selectColorStyleByClassifyNoTree(code, clazzID);
    }

    /**
     * 根据平台类型获取树结构的所有品类-款式
     *
     * @param code
     * @return
     */
    @RequestMapping(value = "/clazzlist/{code}", method = RequestMethod.GET)
    @ApiOperation(value = "根据平台类型获取树结构的所有品类-款式")
    public Object loadAll(@PathVariable String code) {
        return classifyService.selectColorStyleByClassify(code, 0, null);
    }

    /**
     * 根据父品类ID查询带有佣金信息的下一级品类
     *
     * @param parentid
     * @return
     */
    @RequestMapping(value = "/cclist/{parentid}/{shopID}", method = RequestMethod.GET)
    @ApiOperation(value = "根据父品类ID查询带有佣金信息的下一级品类")
    public Object loadDistributionCase(@PathVariable String parentid, @PathVariable String shopID) {
        if (parentid.equals("0")) {
            parentid = "";
        }
        return classifyService.loadDistributionCase(parentid, shopID);
    }


    @RequestMapping(method = RequestMethod.POST)
    @ApiOperation(value = "新增品类")
    public Object add(@Valid @RequestBody Classify record) {
        if (record == null) {
            throw new ServiceException("参数为空", RestConst.ErrorCode.DATABASE_ERROR);
        }
        record.setClazzid(UuidUtils.uuid());
        record.setIsstop(0);
        return classifyService.insertSelective(record);
    }

    @RequestMapping(method = RequestMethod.PUT)
    @ApiOperation(value = "修改品类")
    public Object update(@Valid @RequestBody Classify record) {
        if (record == null) {
            throw new ServiceException("参数为空", RestConst.ErrorCode.DATABASE_ERROR);
        }
        classifyService.selectByPrimaryKey(record.getClazzid());
        return classifyService.updateByPrimaryKeySelective(record);
    }

    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    @ApiOperation(value = "删除ID品类")
    public Object delete(@PathVariable String id) {
        if (id == null || id == "") {
            throw new ServiceException("参数为空", RestConst.ErrorCode.DATABASE_ERROR);
        }
        //判断是否为存在子类
        List classifyList = (List) classifyService.selectByclazzparentid(id);
        if (classifyList.size() != 0) {
            throw new ServiceException("非末端品类", RestConst.ErrorCode.DATABASE_ERROR);
        }
        //判断是否存在属性关联
        Map<String, String> parm = new HashMap<String, String>();
        parm.put("clazzObjID", id);
        if (!classifyToAttrDefService.selectByClazzAndAttr(parm).isEmpty()) {
            throw new ServiceException("存在属性关联", RestConst.ErrorCode.DATABASE_ERROR);
        }
        //判断是否存在款式关联
        if (!classifyStyleService.loadByClassify(id).isEmpty()) {
            throw new ServiceException("存在款式关联", RestConst.ErrorCode.DATABASE_ERROR);
        }
        classifyService.deleteByPrimaryKey(classifyService.selectByPrimaryKey(id));
        return true;
    }

    @RequestMapping(value = "/loadByID/{id}", method = RequestMethod.GET)
    @ApiOperation(value = "查询ID品类详情")
    public Object loadByID(@PathVariable String id) {
        if (id == null || id == "") {
            throw new ServiceException("参数为空", RestConst.ErrorCode.DATABASE_ERROR);
        }
        return classifyService.selectByPrimaryKey(id);
    }

    /**
     * 查询多个ID品类详情
     *
     * @param ids
     * @return
     */
    @RequestMapping(value = "/loadByIDList", method = RequestMethod.POST)
    @ApiOperation(value = "查询多个ID品类详情")
    public Object loadByID(@RequestBody List<String> ids) {
        return classifyService.selectByIDs(ids);
    }

    /**
     * 根据父id查询其所有的子品类
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/loadByParID/{id}", method = RequestMethod.GET)
    @ApiOperation(value = "根据父id查询其下一级品类列表")
    public Object loadByParID(@PathVariable String id) {
        if (id == null || id.equals("")) {
            throw new ServiceException("参数为空", RestConst.ErrorCode.DATABASE_ERROR);
        }
        return classifyService.selectByclazzparentid(id);
    }

    /**
     * 根据分类id和使用平台查询其末端品类的所有款式
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/relationlist/{id}", method = RequestMethod.GET)
    @ApiOperation(value = "根据分类id和使用平台查询其末端品类的所有款式")
    public Object loadStyleRelationInfo(@PathVariable String id) {
        if (id == null || id == "") {
            throw new ServiceException("参数为空", RestConst.ErrorCode.DATABASE_ERROR);
        }

        //判断品类id是否为末端品类
        if (classifyService.selectByclazzparentid(id) == null) {
            throw new ServiceException("非末端品类", RestConst.ErrorCode.DATABASE_ERROR);
        }
        //根据品类id查询款式
        List<Map<String, Object>> stylelist = new ArrayList<Map<String, Object>>();

        stylelist = classifyStyleService.loadByClassify(id);

        return stylelist;
    }


    @RequestMapping(value = "/platformType", method = RequestMethod.GET)
    @ApiOperation(value = "获取平台列表")
    public Object selectAllPlatform() {
        return classifyService.selectAllPlatform();

    }

}
